package com.dongdongshop;

import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @Description
 * @ClassName LogAspect
 * @Author RC
 * @date 2020.12.09 20:38
 */
@Component
@Aspect
public class LogAspect {

    private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);

    //定义切点表达式
    @Pointcut("execution(public * com.dongdongshop.service.*.*(..))")
    public void aopLog(){
    }

    //定义前置通知
    @Before("aopLog()")
    public void doBefore(JoinPoint joinPoint){
        //参数获取
        Object[] args = joinPoint.getArgs();
        //获取调用的方法
        Signature signature = joinPoint.getSignature();
        logger.info("正在进入 {} 方法,参数为: {}",signature,args);
    }

    //定义返回后通知
    @AfterReturning(pointcut = "aopLog()",returning = "result")
    public void doAfterReturn(Object result)throws Throwable{
        //打印返回的结果
        logger.info("方法执行成功,返回的结果为: {}", JSONObject.toJSONString(result));
    }
}
